package cn.felord;

import cn.felord.entity.PayRequest;
import cn.felord.strategy.PayStrategy;

import java.util.Objects;

/**
 * The type Pay context.
 *
 * @author felord
 * @since 13 :41
 */
public class PayContext {
    private PayStrategy payStrategy;


    /**
     * Sets pay strategy.
     *
     * @param payStrategy the pay strategy
     */
    public void setPayStrategy(PayStrategy payStrategy) {
        this.payStrategy = payStrategy;
    }

    /**
     * Pay boolean.
     *
     * @param request 入参
     * @return the boolean
     */
    public boolean pay(PayRequest request) {

        if (Objects.isNull(payStrategy)) {
            throw new IllegalStateException("no strategy");
        }
        //todo 前置处理
        boolean execute = payStrategy.execute(request);
        //todo 后置处理
        return execute;
    }
}
